package edu.nju.cineplex.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import edu.nju.cineplex.dao.DaoHelper;
import edu.nju.cineplex.dao.MovieDao;
import edu.nju.cineplex.model.Movie;

public class MovieDaoImpl implements MovieDao {

	private static MovieDaoImpl movieDao = new MovieDaoImpl();
	private static DaoHelper daoHelper = DaoHelperImpl.getBaseDaoInstance();

	private MovieDaoImpl() {

	}

	public static MovieDaoImpl getInstance() {
		return movieDao;
	}

	@Override
	public void addMovie(Movie movie) {
		// TODO Auto-generated method stub
		Connection con = daoHelper.getConnection();
		PreparedStatement stmt = null;
		try {
			stmt = con
					.prepareStatement("insert into movie(mName,mType,mLength,mLanguage,mPrice ) values(?,?,?,?,?)");
			stmt.setString(1, movie.getmName());
			stmt.setString(2, movie.getmType());
			stmt.setInt(3, movie.getmLength());
			stmt.setString(4, movie.getmLanguage());
			stmt.setDouble(5, movie.getmPrice());
			stmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			daoHelper.closePreparedStatement(stmt);
			daoHelper.closeConnection(con);
		}
	}

	@Override
	public void deleteMovie(Movie movie) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateMovie(Movie movie) {
		// TODO Auto-generated method stub

	}

	@Override
	public Movie findByName(String mName) {
		// TODO Auto-generated method stub
		Connection con = daoHelper.getConnection();
		PreparedStatement stmt = null;
		ResultSet result = null;
		try {
			stmt = con.prepareStatement("select * from movie where  mName = ?");
			stmt.setString(1, mName);
			result = stmt.executeQuery();
			if (result.next()) {
				Movie movie = new Movie();
				movie.setId(result.getLong(1));
				movie.setmName(result.getString(2));
				movie.setmLength(result.getInt(3));
				movie.setmType(result.getString(4));
				movie.setmLanguage(result.getString(5));
				movie.setmPrice(result.getDouble(6));
				return movie;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
			daoHelper.closeResultSet(result);
		}
		return null;
	}

	@Override
	public List<Movie> findAllMovie() {
		// TODO Auto-generated method stub
		Connection con = daoHelper.getConnection();
		PreparedStatement stmt = null;
		ResultSet result = null;
		List<Movie> allMovie = new ArrayList<Movie>();
		try{
			stmt = con.prepareStatement("select * from movie");
			result = stmt.executeQuery();
			while(result.next()){
				Movie movie = new Movie();
				movie.setId(result.getLong(1));
				movie.setmName(result.getString(2));
				movie.setmLength(result.getInt(3));
				movie.setmType(result.getString(4));
				movie.setmLanguage(result.getString(5));
				movie.setmPrice(result.getDouble(6));
				allMovie.add(movie);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			daoHelper.closeResultSet(result);
			daoHelper.closePreparedStatement(stmt);
			daoHelper.closeConnection(con);
		}
		return allMovie;
	}

	@Override
	public Movie findById(long id) {
		// TODO Auto-generated method stub
		Connection con = daoHelper.getConnection();
		PreparedStatement stmt = null;
		ResultSet result = null;
		try {
			stmt = con.prepareStatement("select * from movie where  id = ?");
			stmt.setLong(1, id);
			result = stmt.executeQuery();
			if (result.next()) {
				Movie movie = new Movie();
				movie.setId(result.getLong(1));
				movie.setmName(result.getString(2));
				movie.setmLength(result.getInt(3));
				movie.setmType(result.getString(4));
				movie.setmLanguage(result.getString(5));
				movie.setmPrice(result.getDouble(6));
				return movie;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
			daoHelper.closeResultSet(result);
		}
		return null;
	}

}
